package com.cy.controller;


import com.cy.bfzcommons.service.CartService;
import com.cy.bfzcommons.web.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cart")
@Api(
        value = "/cart",description = "CartController"
        ,tags = "购物车模块"
)
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * (格式)http://localhost/cart/82/52
     * @param productId
     * @param userId
     * @return
     */
    //添加到购物车

    @PostMapping("/{userId}/{productId}")
    public JsonResult saveCart(@ApiParam(name="productId",value="商品id",required=true) @PathVariable Integer productId , @ApiParam(name="userId",value="用户id",required=true) @PathVariable Integer userId){
        String s = cartService.saveCart(productId,userId);
        if(s == null) return JsonResult.ok();
        return JsonResult.fail(s);
    }

    //查看购物车

    @GetMapping("/{id}")
    public JsonResult findCart(@ApiParam(name="id",value="购物车id",required=true) @PathVariable Integer id){
       return JsonResult.ok(cartService.findCart(id));
    }

    //删除购物车物品

    @DeleteMapping("/{userId}/{productId}")
    public JsonResult deleteCart(@ApiParam(name="productId",value="商品id",required=true)@PathVariable Integer productId ,
                                 @ApiParam(name="userId",value="用户id",required=true) @PathVariable Integer userId){
        String s = cartService.deleteCart(productId,userId);
        if(s == null){
            return JsonResult.ok();
        }
        return JsonResult.fail(s);
    }





}
